#include "bsp_exti.h"
#include "bsp_A_phase_zero_point_tim.h"
#include "bsp_B_phase_zero_point_tim.h"
#include "bsp_C_phase_zero_point_tim.h"
#include "stdio.h"

/*******************************************这个C文件主要用于过零点的触发***********************************************/
uint16_t A_Phase_Rising_Edge_To_Zero_Point=A_PHASE_RISING_EDGE_TO_ZERO_POINT;
uint16_t B_Phase_Rising_Edge_To_Zero_Point=B_PHASE_RISING_EDGE_TO_ZERO_POINT;
uint16_t C_Phase_Rising_Edge_To_Zero_Point=C_PHASE_RISING_EDGE_TO_ZERO_POINT;
/**
  * @brief  配置嵌套向量中断控制器NVIC
  * @param  无
  * @retval 无
  */
static void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	//A相
	NVIC_InitStructure.NVIC_IRQChannel = A_PHASE_INT_EXTI_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	//B相
	NVIC_InitStructure.NVIC_IRQChannel = B_PHASE_INT_EXTI_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	// //C相   由于C相和B相都同在EXTI0_1_IRQn上故只配置B相就够了
	// NVIC_InitStructure.NVIC_IRQChannel=C_PHASE_INT_EXTI_IRQ;
	// NVIC_InitStructure.NVIC_IRQChannelPriority=3;
	// NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	// NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  A相,B相,C相 三相上升沿外部中断配置
  * @param  无
  * @retval 无
  */
void EXTI_ABC_Phase_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	EXTI_InitTypeDef EXTI_InitStructure;

	//开启ABC三相GPIO管脚时钟
	RCC_AHBPeriphClockCmd(A_PHASE_INT_GPIO_CLK | B_PHASE_INT_GPIO_CLK | C_PHASE_INT_GPIO_CLK, ENABLE);
	//配置NVIC中断
	NVIC_Configuration();

	/******************A相配置*******************************/
	GPIO_InitStructure.GPIO_Pin = A_PHASE_INT_GPIO_PIN;
	//既然是输入那就得为浮空输入
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	//因为是输入故不需要速度
	GPIO_Init(A_PHASE_INT_GPIO_PORT, &GPIO_InitStructure);
	/* Enable SYSCFG clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
	/* Connect EXTI2 Line to PB2 pin */
	SYSCFG_EXTILineConfig(A_PHASE_INT_EXTI_PORTSOURCE, A_PHASE_INT_EXTI_PINSOURCE);
	//初始化EXTI_InitStructure结构体
	EXTI_InitStructure.EXTI_Line = A_PHASE_INT_EXTI_LINE;  //外部中断线
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	   //中断模式
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;			   //使能中断
	//开始配置
	EXTI_Init(&EXTI_InitStructure);

	/******************B相配置*******************************/
	GPIO_InitStructure.GPIO_Pin = B_PHASE_INT_GPIO_PIN;
	//既然是输入那就得为浮空输入
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	//因为是输入故不需要速度
	GPIO_Init(B_PHASE_INT_GPIO_PORT, &GPIO_InitStructure);
	/* Enable SYSCFG clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
	/* Connect EXTI1 Line to PB1 pin */
	SYSCFG_EXTILineConfig(B_PHASE_INT_EXTI_PORTSOURCE, B_PHASE_INT_EXTI_PINSOURCE);
	//初始化EXTI_InitStructure结构体
	EXTI_InitStructure.EXTI_Line = B_PHASE_INT_EXTI_LINE;  //外部中断线
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	   //中断模式
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;			   //使能中断
	//开始配置
	EXTI_Init(&EXTI_InitStructure);

	/******************C相配置*******************************/
	GPIO_InitStructure.GPIO_Pin = C_PHASE_INT_GPIO_PIN;
	//既然是输入那就得为浮空输入
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	//因为是输入故不需要速度
	GPIO_Init(C_PHASE_INT_GPIO_PORT, &GPIO_InitStructure);
	/* Enable SYSCFG clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
	/* Connect EXTI0 Line to PB0 pin */
	SYSCFG_EXTILineConfig(C_PHASE_INT_EXTI_PORTSOURCE, C_PHASE_INT_EXTI_PINSOURCE);
	//初始化EXTI_InitStructure结构体
	EXTI_InitStructure.EXTI_Line = C_PHASE_INT_EXTI_LINE;  //外部中断线
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	   //中断模式
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;			   //使能中断
	//开始配置
	EXTI_Init(&EXTI_InitStructure);
}

/**
  * @brief  三相信号指示
  * @param  无
  * @retval 无
  */
void Indicator_Pin_Config(void)
{

	/*定义一个GPIO_InitTypeDef类型的结构体*/
	GPIO_InitTypeDef GPIO_InitStructure; //(GPIO初始化类型定义)

	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
	/*开始配置相关的GPIO端口*/
	//因为配置的是同一种模式，故用1个初始化结构体即可  (这里配置的是LED彩灯)
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	/*调用库函数初始化GPIO*/
	//因要配置的GPIO引脚都在同一个端口故，只用GPIOB
	GPIO_Init(GPIOB, &GPIO_InitStructure);
}

/**
  * @brief  外部中断函数
  * @param  无
  * @retval 无
  */
void A_PHASE_IRQHandler(void)
{
	//确保是否产生了EXTI Line中断
	if (EXTI_GetITStatus(A_PHASE_INT_EXTI_LINE) != RESET)
	{
		A_PHASE_INDICATOR_TOGGLE;
		//上升沿中断中只配置零点定时器
		A_PHASE_ZERO_POINT_TIM->ARR =A_Phase_Rising_Edge_To_Zero_Point+3;
		A_PHASE_ZERO_POINT_TIM->CNT = 0;
		TIM_Cmd(A_PHASE_ZERO_POINT_TIM, ENABLE);
		EXTI_ClearITPendingBit(A_PHASE_INT_EXTI_LINE);
	}
}

/**
  * @brief  外部中断函数
  * @param  无
  * @retval 无
  */
void B_PHASE_IRQHandler(void)
{
	//确保是否产生了EXTI Line中断
	if (EXTI_GetITStatus(B_PHASE_INT_EXTI_LINE) != RESET)
	{
		B_PHASE_INDICATOR_TOGGLE;
		//上升沿中断中只配置零点定时器
		B_PHASE_ZERO_POINT_TIM->ARR = B_Phase_Rising_Edge_To_Zero_Point+3;
		B_PHASE_ZERO_POINT_TIM->CNT = 0;
		TIM_Cmd(B_PHASE_ZERO_POINT_TIM, ENABLE);
		EXTI_ClearITPendingBit(B_PHASE_INT_EXTI_LINE);
	}
	//确保是否产生了EXTI Line中断
	if (EXTI_GetITStatus(C_PHASE_INT_EXTI_LINE) != RESET)
	{
		C_PHASE_INDICATOR_TOGGLE;
		//上升沿中断中只配置零点定时器
		C_PHASE_ZERO_POINT_TIM->ARR = C_Phase_Rising_Edge_To_Zero_Point+3;
		C_PHASE_ZERO_POINT_TIM->CNT = 0;
		TIM_Cmd(C_PHASE_ZERO_POINT_TIM, ENABLE);
		EXTI_ClearITPendingBit(C_PHASE_INT_EXTI_LINE);
	}
}

//  /**
//   * @brief  外部中断函数   因为B相和C相共享一个中断函数故统一写在B_PHASE_IRQHandler中
//   * @param  无
//   * @retval 无
//   */
// void C_PHASE_IRQHandler(void)
// {
//   //确保是否产生了EXTI Line中断
// 	if(EXTI_GetITStatus(C_PHASE_INT_EXTI_LINE) != RESET)
// 	{
//     C_PHASE_INDICATOR_TOGGLE;
//     //上升沿中断中只配置零点定时器
//     C_PHASE_ZERO_POINT_TIM->ARR =C_PHASE_RISING_EDGE_TO_ZERO_POINT - 1;
//     C_PHASE_ZERO_POINT_TIM->CNT =0;
//     TIM_Cmd(C_PHASE_ZERO_POINT_TIM, ENABLE);
// 		EXTI_ClearITPendingBit(C_PHASE_INT_EXTI_LINE);
// 	}
// }

/*********************************************END OF FILE**********************/
